<?php use_helper('jQuery')?>

<?php use_javascripts_for_form($form) ?>
<?php use_stylesheets_for_form($form) ?>

<?php echo jq_javascript_tag()?>
    initPortalFileUpload(
        '<?php echo url_for('portal_fileupload').(!$form->getObject()->isNew() ? '?portal_id='.$form->getObject()->getId() : '')?>'
        ,
        '<?php echo !$form->getObject()->isNew() ? '/uploads/portal_'.$form->getObject()->getId().'/temp_files/':'/uploads/temp_files/'?>'
    );

    function displayMaxCommentsDropList(){
        if($('input#ems_portal_uses_content_comments_1').is(':checked')){
            $('div#max_comments_per_page').show()
        } else {
            $('div#max_comments_per_page').hide()
        }
    }

    function bindChangeEvent(){
        $('input#ems_portal_uses_content_comments_1').change(
            function(){
                if($(this).is(':checked')){
                    $('input#ems_portal_max_comments_per_page').val("5");
                    $('div#max_comments_per_page').fadeIn("slow");
                }
            }
        );
        $('input#ems_portal_uses_content_comments_0').change(
            function(){
                if($(this).is(':checked')){
                    $('div#max_comments_per_page').fadeOut("slow",
                        function(){
                            $('input#ems_portal_max_comments_per_page').val("3");
                        }
                    );
                }
            }
        );
    }

    displayMaxCommentsDropList();
    bindChangeEvent();

    $('#cancel_button').click(
        function(){
            $('#component_right').empty();
    })
<?php echo jq_end_javascript_tag()?>

<?php if($form->getObject()->exists()):?>
    <div id="main_header" class="ui-widget-header ui-corner-all">
        <h3>Editando Portal: "<?php echo $form->getObject()->getTitle() ?>"</h3>
    </div>
    <div id="main_content" class="ui-widget ui-widget-content ui-corner-all">
<?php endif;?>
    <div class="form">
        <?php echo jq_form_remote_tag(
                array(
                    'update' => ($form->getObject()->isNew() ? 'component_right':'ajax_holder_for_multi_update'),
                    'url' => ($form->getObject()->isNew() ?
                                url_for('portal_create',$form->getObject())
                                    :
                                url_for('portal_update', $form->getObject())
                            ),
                    'before'   => ($form->getObject()->isNew() ?
                                    'hideComponentRightContent();showLoaderComponentRight()'
                                        :
                                    'hideMainContent();showLoaderMain();'),
                    'complete' => ($form->getObject()->isNew() ? 
                                    'hideLoader();showComponentRightContent()'
                                        : 
                                    'hideLoader();showMainContent();'),
                    'script'   => true
                ),array(
                    'id' => 'file_upload'
                ))?>
        
        <?php if (!$form->getObject()->isNew()): ?>
            <input type="hidden" name="sf_method" value="put" />
        <?php endif; ?>
            <?php echo $form->renderHiddenFields() ?>
            <?php echo $form->renderGlobalErrors() ?>
            <fieldset>
                <div class="form-row first-row">
                    <?php echo $form['title']->renderError() ?>
                    <?php echo $form['title']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['title']->render() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['website_url']->renderError() ?>
                    <?php echo $form['website_url']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['website_url']->render() ?>
                        <?php echo $form['website_url']->renderHelp() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['email']->renderError() ?>
                    <?php echo $form['email']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['email']->render() ?>
                        <?php echo $form['email']->renderHelp() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['image']->renderError() ?>
                    <?php echo $form['image_type']->renderError() ?>
                    <?php echo $form['image_size']->renderError() ?>
                    <?php echo $form['tmp_image']->renderError() ?>
                    <table id="<?php echo 'files'?>" class="upload-files-table">
                        <?php if ($form['image']->getValue()): ?>
                            <tr>
                                <td>Archivo actual:</td>
                                <td class="image-preview">
                                    <?php if (!$form->getObject()->isNew()): ?>
                                        <img alt="" src="<?php echo '/uploads/portal_'.
                                            $form->getObject()->getId().
                                            ($form->getObject()->getImage() ?
                                                (!$form->isBound() ?
                                                    '/images/'
                                                    :
                                                    ($form['image']->getValue() == $form->getObject()->getImage()
                                                        ?
                                                        '/images/'
                                                        :
                                                        '/temp_files/'
                                                    )
                                                )
                                                :
                                                '/temp_files/'
                                            ).
                                            basename($form['image']->getValue())
                                        ?>">
                                    <?php else: ?>
                                        <img alt="" src="<?php echo '/uploads/temp_files/'.basename($form['image']->getValue())?>">
                                    <?php endif; ?>
                                </td>
                                <td class="image-size">
                                    Tamaño del archivo: <?php echo round(($form['image_size']->getValue()/1024),2) ?> Kb
                                </td>
                            </tr>
                        <?php endif;?>
                    </table>
                    <?php echo $form['tmp_image']->renderLabel() ?>
                    <div id="<?php echo 'drop_zone'?>" class="form-field drop-zone">
                        <?php echo $form['tmp_image']->render() ?>
                        <?php echo $form['tmp_image']->renderHelp() ?>
                        <div>Archivo a cargar</div>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['downloadable_contents_preview']->renderError() ?>
                    <?php echo $form['downloadable_contents_preview']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['downloadable_contents_preview']->render() ?>
                        <?php echo $form['downloadable_contents_preview']->renderHelp() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['uses_register']->renderError() ?>
                    <?php echo $form['uses_register']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['uses_register']->render() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['uses_serv_buttons']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['uses_serv_buttons']->renderError() ?>
                        <?php echo $form['uses_serv_buttons']->render() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['uses_content_comments']->renderError() ?>
                    <?php echo $form['uses_content_comments']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['uses_content_comments']->render() ?>
                    </div>
                </div>
                <div id="max_comments_per_page" class="form-row"
                     style="display:
                        <?php echo ($form->getObject()->getUsesContentComments() ? 'block' : 'none')?>">
                    <?php echo $form['max_comments_per_page']->renderError() ?>
                    <?php echo $form['max_comments_per_page']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['max_comments_per_page']->render() ?>
                    </div>
                </div>
                <?php if(isset($form['organization_id'])): ?>
                    <div class="form-row">
                        <?php echo $form['organization_id']->renderError() ?>
                        <?php echo $form['organization_id']->renderLabel() ?>
                        <div class="form-field">
                            <?php echo $form['organization_id']->render() ?>
                        </div>
                    </div>
                <?php endif; ?>
                <!--
                    Esto es para el css que utilizará el portal
                    Estará comentado por ahora
                <div class="form-row">
                    <?php echo $form['css_id']->renderError() ?>
                    <?php echo $form['css_id']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['css_id']->render() ?>
                    </div>
                </div>
                -->
                <?php if(isset ($form['published'])): ?>
                    <div class="form-row">
                        <?php echo $form['published']->renderError() ?>
                        <?php echo $form['published']->renderLabel() ?>
                        <div class="form-field">
                            <?php echo $form['published']->render() ?>
                        </div>
                    </div>
                <?php endif; ?>
                <?php if(isset ($form['is_enabled'])): ?>
                    <div class="form-row">
                        <?php echo $form['is_enabled']->renderError() ?>
                        <?php echo $form['is_enabled']->renderLabel() ?>
                        <div class="form-field">
                            <?php echo $form['is_enabled']->render() ?>
                        </div>
                    </div>
                <?php endif; ?>
            </fieldset>
            <div id="form_buttons" class="ui-widget">
                    <input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-button-text"
                           type="submit" value="Guardar" title="Guardar"/>
                    <?php if($form->getObject()->isNew()):?>
                        <a id="cancel_button" href="#" title="Cancelar" aria-disabled="false"
                            class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">
                            <span class="ui-button-text">Cancelar</span>
                        </a>
                    <?php else: ?>
                        <?php echo jq_link_to_remote(
                                '<span class="ui-button-text">Cancelar</span>',
                            array(
                                'update' => 'main',
                                'url' => url_for('portal_show', $form->getObject()),
                                'before' => 'clearView();showLoaderMain();',
                                'complete' => 'hideLoader();',
                                'script' => true
                            ), array(
                                'class' => 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only',
                                'title' => 'Cancelar'
                            )) ?>
                    <?php endif; ?>
            </div>
        </form>
    </div>
<?php if($form->getObject()->exists()):?>
    </div>
<?php endif; ?>